<template>
  <div class="simi">
    <div class="title">
      <div class="icon"/>
      {{ title }}
    </div>
    <div class="body">
      <div class="view">
        <transition v-for="good in activeGoods"
                    enter-active-class="animate__animated animate__fadeIn"
                    leave-active-class="animate__animated animate__fadeOut"
                    mode="out-in">
          <simi-item :key="good.id"
                     :data="good"/>
        </transition>
      </div>
      <i class="iconfont icon-angle-left prev"
         @click="simiIndex = (totalPage + simiIndex - 1) % totalPage"/>
      <i class="iconfont icon-angle-right next"
         @click="simiIndex = (simiIndex + 1) % totalPage"></i>
      <p class="slider">
        <span v-for="(item, index) in totalPage"
              :key="item"
              :class="{active: index === simiIndex}"
              @click="simiIndex = index"/>
      </p>
    </div>
  </div>
</template>

<script lang="ts"
        setup>
import SimiItem from '@/components/library/SimiItem.vue'
import type {Good} from '@/types/good'
import type {Ref} from 'vue'
import {computed, inject, ref, toRefs} from 'vue'

let props = defineProps<{
  title: string,
  data?: Good[]
}>()
let {data: goods} = toRefs(props)

let simiIndex = <Ref>inject('simiIndex') ?? ref<number>(0)
let size = 4

let totalPage = computed(() => {
  return Math.ceil(<number>goods?.value?.length / size)
})
let activeGoods = computed((): Good[] => {
  return goods?.value?.slice(simiIndex.value * size, (simiIndex.value + 1) * size) ?? []
})
</script>

<style lang="less"
       scoped>
.simi {
  background-color: #fff;

  .title {
    display: flex;
    align-items: center;
    height: 80px;
    font-size: 20px;
    padding: 0 20px;

    .icon {
      width: 16px;
      height: 16px;
      margin-right: 10px;
      border-bottom: 4px solid @xtxColor;
      border-right: 4px solid @xtxColor;
      transform: rotate(-45deg);

      &:before {
        content: "";
        display: block;
        width: 10px;
        height: 10px;
        background-color: #bcf1e6;
      }
    }
  }

  .body {
    height: 380px;
    position: relative;
    padding: 0 40px;

    i {
      font-size: 30px;
      cursor: pointer;
      color: #ddd;
      top: 110px;
      position: absolute;

      &.prev {
        left: 60px;
      }

      &.next {
        right: 60px;
      }
    }

    .view {
      display: flex;
      justify-content: space-evenly;
    }

    .slider {
      height: 90px;
      display: flex;
      align-items: center;
      justify-content: center;

      span {
        display: block;
        width: 12px;
        height: 12px;
        border-radius: 50%;
        background-color: rgba(0, 0, 0, .2);
        cursor: pointer;

        &.active {
          background-color: @xtxColor;
        }

        & ~ span {
          margin-left: 10px;
        }
      }
    }
  }
}
</style>